<template>
  <div class="user-box">
    <el-button size="mini" @click="dialogVisible = true">新增</el-button>

    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form
        :rules="rules"
        :inline="true"
        ref="form"
        :model="form"
        label-width="80px"
      >
        <el-form-item prop="name" label="姓名">
          <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item prop="age" label="年龄">
          <el-input placeholder="请输入年龄" v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="birth" label="出生日期">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.birth"
          ></el-date-picker>
        </el-form-item>

        <el-form-item prop="addr" label="地址">
          <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 用户列表 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.sex === 1 ? "男" : "女"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="birth" label="出生日期" width="180">
      </el-table-column>
      <el-table-column prop="addr" label="地址" width="180"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
         <el-button size="mini" @click="handelEdit(scope.row)">编辑</el-button>
         <el-button size="mini" type="danger" @click="handelDel(scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getUser } from "../api/idnex.js";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        birth: [{ required: true, message: "请输入出生日期", trigger: "blur" }],
        addr: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
      tableData: [],
    };
  },
  methods: {
    //提交用户表单
    submit() {
      this.$refs.form.validate((val) => {
        if (val) {
          console.log(this.form);

          // 清除表单
          this.$refs.form.resetFields();
          // 关闭弹窗
          this.dialogVisible = false;
        }
      });
    },
    // 关闭弹窗
    handleClose() {
      // 清除表单
      this.$refs.form.resetFields();
      // 关闭弹窗
      this.dialogVisible = false;
    },
    getList() {
      getUser().then(({ data }) => {
        console.log(data);
        this.tableData = data.list;
      });
    },
    // 编辑
    handelEdit(){

    },
       // 删除
    handelDel(){

    }
  },
  mounted() {
    this.getList();
  },
};
</script>
